package com.zadtek.shopmall.activities;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.youth.banner.Banner;
import com.youth.banner.loader.ImageLoader;
import com.zadtek.shopmall.Event.EventTest;
import com.zadtek.shopmall.R;
import com.zadtek.shopmall.base.AbsActivity;
import com.zadtek.shopmall.bean.InfoType;
import com.zadtek.shopmall.bean.OperateResponseBean2;
import com.zadtek.shopmall.bean.UpLoadImgResponse;
import com.zadtek.shopmall.net.BaseObserver;
import com.zadtek.shopmall.net.BaseResponse;
import com.zadtek.shopmall.net.NetTool;
import com.zadtek.shopmall.utils.ToastUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

import static com.zadtek.shopmall.activities.ChoosePicActivity.IMGS;

public class PublishInfoActivity extends AbsActivity implements View.OnClickListener {

    private ImageView ivAdd;
    private Banner banner;
    private ImageView ivAddSmall;
    private EditText etTitle;
    private EditText etContent;
    private EditText etName;
    private EditText etPhone;
    private TextView tvBtnSubmit;
    private ImageView icBack;
    private TextView topTitle;
    private TextView chooseInfo;
    private ArrayList<String> choosePicList = new ArrayList<>();
    private ArrayList<String> chooseLocalPicList = new ArrayList<>();
    private InfoType infoType;

    private void initView() {
        ivAdd = (ImageView)findViewById( R.id.iv_add );
        icBack = findViewById(R.id.ic_back);
        topTitle = findViewById(R.id.title);
        banner = (Banner)findViewById( R.id.banner );
        ivAddSmall = (ImageView)findViewById( R.id.iv_add_small );
        etTitle = (EditText)findViewById( R.id.et_title );
        etContent = (EditText)findViewById( R.id.et_content );
        etName = (EditText)findViewById( R.id.et_name );
        etPhone = (EditText)findViewById( R.id.et_phone );
        tvBtnSubmit = (TextView)findViewById( R.id.tv_btn_submit );
        chooseInfo = findViewById(R.id.tv_chosse_info);
        icBack.setVisibility(View.VISIBLE);
        topTitle.setText("发布信息");
    }

    @Override
    public int setView() {
        return R.layout.activity_publish_info;
    }

    @Override
    public void initBasic(Bundle savedInstanceState) {
        infoType = (InfoType) getIntent().getSerializableExtra("data");
        EventBus.getDefault().register(this);
        initView();
        initLogic();
        if(infoType!=null){
            chooseInfo.setText("你选择的分类是:  "+infoType.getInfoString());
        }
    }

    private void initLogic() {
        icBack.setOnClickListener(this);
        ivAdd.setOnClickListener(this);
        ivAddSmall.setOnClickListener(this);
        tvBtnSubmit.setOnClickListener(this);
        banner.setImageLoader(new GlideImageLoader());

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_btn_submit:
                submit();
                break;
            case R.id.ic_back:
                finish();
                break;
            case R.id.iv_add_small:
            case R.id.iv_add:
                startActivityForResult(new Intent(this,ChoosePicActivity.class).putStringArrayListExtra(IMGS,chooseLocalPicList),201);
                break;
        }
    }

    private void submit() {
        String title = etTitle.getText().toString();
        String description = etContent.getText().toString();
        String phone = etPhone.getText().toString();
        String name = etName.getText().toString();
        if(TextUtils.isEmpty(title)){
            ToastUtil.showToast("请填写标题！");
            return;
        }
        if(TextUtils.isEmpty(description)){
            ToastUtil.showToast("请填写内容！");
            return;
        }
        if(TextUtils.isEmpty(phone)){
            ToastUtil.showToast("请填写电话！");
            return;
        }
        if(TextUtils.isEmpty(name)){
            ToastUtil.showToast("请填写名字！");
            return;
        }
        Map<String, Object> param = NetTool.newParams();
        param.put("type",infoType.getId());
        param.put("title",title);
        param.put("description",description);
        param.put("name",name);
        param.put("phone",phone);
        param.put("image",getImgs());
        NetTool.getApi().publishInfo(param).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse<OperateResponseBean2>>(this) {
                    @Override
                    public void onData(BaseResponse<OperateResponseBean2> value) {
                        if(value.getCode() == 0 && value.getData().isSuccess()){
                            ToastUtil.showToast("发布成功！");
                            startActivity(new Intent(mActivity,PublishInfoDownActivity.class));
                        }else{
                            ToastUtil.showToast("发布失败"+value.getData().getMessage());
                        }
                    }
                });
    }

    private String getImgs() {
        String imgs = "";
        for(String path:choosePicList){
            imgs= imgs+path+"@";
        }
        return imgs;
    }

    @Subscribe
    public void onEventMainThread(EventTest event) {
        if(event.getCode() == 1) finish();
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == RESULT_OK && requestCode == 201){
            ArrayList<String> list = data.getStringArrayListExtra(IMGS);
            choosePicList.clear();
            chooseLocalPicList.clear();
            chooseLocalPicList.addAll(list);
            NetTool.uploadFile(list).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new BaseObserver<BaseResponse<UpLoadImgResponse>>(this) {
                        @Override
                        public void onData(BaseResponse<UpLoadImgResponse> value) {
                            if(value.getCode() == 0 && value.getData()!=null){
                                if(value.getData().getImage().size()!=0){
                                    choosePicList.addAll(value.getData().getImage());
                                    if(choosePicList.size()!=0){
                                        banner.setVisibility(View.VISIBLE);
                                        ivAddSmall.setVisibility(View.VISIBLE);
                                        ivAdd.setVisibility(View.GONE);
                                        banner.setImages(choosePicList);
                                        banner.start();
                                    }
                                }
                            }
                        }
                    });

        }
    }

    public class GlideImageLoader extends ImageLoader {
        @Override
        public void displayImage(Context context, Object path, ImageView imageView) {
            Glide.with(context).load(path).into(imageView);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
